/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.ui;

import net.sourceforge.p4eclipse.core.Policy;
import net.sourceforge.p4eclipse.ui.model.AllRootsElement;
import net.sourceforge.p4eclipse.ui.model.RemoteContentProvider;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.DrillDownAdapter;
import org.eclipse.ui.part.ViewPart;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class DepotView extends ViewPart
{
	public static final String ID = PerforceUIPlugin.ID + ".ui.DepotView"; //$NON-NLS-1$
	
	private TreeViewer viewer;
	private DrillDownAdapter drillPart;
	private AllRootsElement root;

	private Action refreshAction;

	/*
	 * @see IWorkbenchPart#createPartControl(Composite)
	 */
	public void createPartControl(Composite parent)
	{
		root = new AllRootsElement();
		
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new RemoteContentProvider());	
		viewer.setLabelProvider(new WorkbenchLabelProvider());
		getSite().setSelectionProvider(viewer);
		
		viewer.setInput(root);
		viewer.getControl().addKeyListener(new KeyAdapter() 
			{
				public void keyPressed(KeyEvent event) 
				{
					if (event.keyCode == SWT.F5) 
						refreshAction.run();
				}
			});
		
		
		drillPart = new DrillDownAdapter(viewer);		
		contributeActions();
	}

	/*
	 * @see IWorkbenchPart#setFocus()
	 */
	public void setFocus()
	{
		Policy.assertNotNull(viewer);
		viewer.getControl().setFocus();
	}

	private void contributeActions()
	{
		viewer.addDoubleClickListener(
			new IDoubleClickListener() 
			{
				public void doubleClick(DoubleClickEvent e) 
				{
					drillPart.goInto();
				}
			});
		
		
		refreshAction = 
			new Action("Refresh View", PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_REFRESH)) //$NON-NLS-1$
			{
				public void run()
				{
					viewer.refresh();
				}
			};
		
		MenuManager menuManager = new MenuManager();
		Tree tree = viewer.getTree();
		Menu menu = menuManager.createContextMenu(tree);
		menuManager.addMenuListener(
			new IMenuListener()
			{
				/*
				 * @see IMenuListener#menuAboutToShow(IMenuManager)
				 */
				public void menuAboutToShow(IMenuManager manager)
				{
					manager.add(new Separator(IWorkbenchActionConstants.GROUP_FILE));
					manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
					manager.add(refreshAction);
				}
			});
		menuManager.setRemoveAllWhenShown(true);
		tree.setMenu(menu);
		getSite().registerContextMenu(menuManager, viewer);	
		
		// Create the local tool bar
		IActionBars bars = getViewSite().getActionBars();
		IToolBarManager tbm = bars.getToolBarManager();
		drillPart.addNavigationActions(tbm);
		tbm.add(refreshAction);
		tbm.update(false);			
	}
}
